package cn.iocoder.yudao.module.shop.controller.admin.giftcard;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.shop.controller.admin.giftitem.vo.ShopGiftItemRespVO;
import cn.iocoder.yudao.module.shop.convert.giftitem.ShopGiftItemConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.giftitem.ShopGiftItemDO;
import cn.iocoder.yudao.module.shop.service.giftitem.ShopGiftItemService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.giftcard.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.giftcard.ShopGiftCardDO;
import cn.iocoder.yudao.module.shop.convert.giftcard.ShopGiftCardConvert;
import cn.iocoder.yudao.module.shop.service.giftcard.ShopGiftCardService;

@Tag(name = "管理后台 - 礼品卡")
@RestController
@RequestMapping("/shop/gift-card")
@Validated
public class ShopGiftCardController {

    @Resource
    private ShopGiftCardService giftCardService;

    @Resource
    private ShopGiftItemService giftItemService;

    @PostMapping("/create")
    @Operation(summary = "创建礼品卡")
    @PreAuthorize("@ss.hasPermission('shop:gift-card:create')")
    public CommonResult<Long> createGiftCard(@Valid @RequestBody ShopGiftCardCreateReqVO createReqVO) {
        return success(giftCardService.createGiftCard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新礼品卡")
    @PreAuthorize("@ss.hasPermission('shop:gift-card:update')")
    public CommonResult<Boolean> updateGiftCard(@Valid @RequestBody ShopGiftCardUpdateReqVO updateReqVO) {
        giftCardService.updateGiftCard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除礼品卡")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:gift-card:delete')")
    public CommonResult<Boolean> deleteGiftCard(@RequestParam("id") Long id) {
        giftCardService.deleteGiftCard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得礼品卡")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:gift-card:query')")
    public CommonResult<ShopGiftCardRespVO> getGiftCard(@RequestParam("id") Long id) {
        ShopGiftCardDO giftCard = giftCardService.getGiftCard(id);
        ShopGiftItemDO itemDO = giftItemService.getGiftItem(giftCard.getGiftId());
        ShopGiftCardRespVO respVO = ShopGiftCardConvert.INSTANCE.convert(giftCard);
        respVO.setGiftItem(ShopGiftItemConvert.INSTANCE.convert(itemDO));

        return success(respVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得礼品卡列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:gift-card:query')")
    public CommonResult<List<ShopGiftCardRespVO>> getGiftCardList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopGiftCardDO> list = giftCardService.getGiftCardList(ids);
        Set<Long> giftList = CollectionUtils.convertSet(list, ShopGiftCardDO::getGiftId);
        List<ShopGiftItemDO> itemDoList = giftItemService.getGiftItemList(giftList);
        Map<Long, ShopGiftItemDO> itemDoMap = CollectionUtils.convertMap(itemDoList, ShopGiftItemDO::getId);
        List<ShopGiftCardRespVO> respVOList = ShopGiftCardConvert.INSTANCE.convertList(list);
        for(int i = 0; i<respVOList.size(); i++){
            ShopGiftItemRespVO itemRespVo = ShopGiftItemConvert.INSTANCE.convert(itemDoMap.get(respVOList.get(i).getGiftId()));
            respVOList.get(i).setGiftItem(itemRespVo);
        }
        return success(respVOList);
    }

    @GetMapping("/page")
    @Operation(summary = "获得礼品卡分页")
    @PreAuthorize("@ss.hasPermission('shop:gift-card:query')")
    public CommonResult<PageResult<ShopGiftCardRespVO>> getGiftCardPage(@Valid ShopGiftCardPageReqVO pageVO) {
        PageResult<ShopGiftCardDO> pageResult = giftCardService.getGiftCardPage(pageVO);
        PageResult<ShopGiftCardRespVO> result = ShopGiftCardConvert.INSTANCE.convertPage(pageResult);
        if(result.getList().size() > 0){
            Set<Long> giftList = CollectionUtils.convertSet(result.getList(), ShopGiftCardRespVO::getGiftId);
            List<ShopGiftItemDO> itemDoList = giftItemService.getGiftItemList(giftList);
            Map<Long, ShopGiftItemDO> itemDoMap = CollectionUtils.convertMap(itemDoList, ShopGiftItemDO::getId);
            for(int i = 0; i<result.getList().size(); i++){
                ShopGiftItemRespVO itemRespVo = ShopGiftItemConvert.INSTANCE.convert(itemDoMap.get(result.getList().get(i).getGiftId()));
                result.getList().get(i).setGiftItem(itemRespVo);
            }
        }
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出礼品卡 Excel")
    @PreAuthorize("@ss.hasPermission('shop:gift-card:export')")
    @OperateLog(type = EXPORT)
    public void exportGiftCardExcel(@Valid ShopGiftCardExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopGiftCardDO> list = giftCardService.getGiftCardList(exportReqVO);
        // 导出 Excel
        List<ShopGiftCardExcelVO> datas = ShopGiftCardConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "礼品卡.xls", "数据", ShopGiftCardExcelVO.class, datas);
    }

    @PostMapping("/active")
    @Operation(summary = "激活礼品卡")
    @PreAuthorize("@ss.hasPermission('shop:gift-card:update')")
    public CommonResult<Boolean> activeCard(@Valid @RequestBody ShopGiftCardActiveReqVO reqVO){
        giftCardService.activeCard(reqVO);
        return success(true);
    }

    @PostMapping("/send")
    @Operation(summary = "发放礼品卡")
    @PreAuthorize("@ss.hasPermission('shop:gift-card:update')")
    public CommonResult<Boolean> sendCard(@Valid @RequestBody ShopGiftCardSendReqVO reqVO){
        giftCardService.sendCard(reqVO);
        return success(true);
    }

    @GetMapping("/statistics")
    @Operation(summary = "礼品卡汇总")
    @PreAuthorize("@ss.hasPermission('shop:gift-card:query')")
    public CommonResult<ShopGiftCardStatisticsRespVO> statisticsCount(@RequestParam("id") Long id){
        ShopGiftCardStatisticsRespVO respVO = giftCardService.statisticsCount(id);

        return success(respVO);
    }

}
